package ihm;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import metier.Tarif;
import dao.DaoProjetCinema;

public class FenetreAjoutTarif extends JInternalFrame implements ActionListener
{
	private JTextField txtLibTarif;
	private JTextField txtPrix;
	private JLabel lblTarif;
	private JLabel lblPrix;
	private JButton btnAjouter;
	private Vector<Tarif> lesTarifs;

	/**
	 * Create the frame.
	 */
	public FenetreAjoutTarif(Vector<Tarif> lesTarifs)
	{
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		
		this.txtLibTarif = new JTextField();
		this.txtLibTarif.setBounds(124, 92, 86, 20);
		getContentPane().add(this.txtLibTarif);
		this.txtLibTarif.setColumns(10);
		
		this.txtPrix = new JTextField();
		this.txtPrix.setBounds(124, 123, 86, 20);
		getContentPane().add(this.txtPrix);
		this.txtPrix.setColumns(10);
		
		this.lblTarif = new JLabel("Tarif :");
		this.lblTarif.setBounds(44, 95, 70, 14);
		getContentPane().add(this.lblTarif);
		
		this.lblPrix = new JLabel("Prix :");
		this.lblPrix.setBounds(44, 126, 60, 14);
		getContentPane().add(this.lblPrix);
		
		this.btnAjouter = new JButton("Ajouter");
		this.btnAjouter.setBounds(297, 211, 91, 23);
		this.btnAjouter.addActionListener(this);
		getContentPane().add(this.btnAjouter);
		
		this.lesTarifs = lesTarifs;
	}
	
	
	public void actionPerformed(ActionEvent evt) 
	{
		Tarif leTarif;
		int nb;
		
		if (evt.getSource() == this.btnAjouter)
		{
			if (this.txtLibTarif.getText().isEmpty() || this.txtPrix.getText().isEmpty())
				JOptionPane.showMessageDialog(null, "Tous les champs sont obligatoires !");
			else
			{
				leTarif = new Tarif(this.txtLibTarif.getText(), Double.parseDouble(this.txtPrix.getText()));
				nb = DaoProjetCinema.ajouterTarif(leTarif);
				
				if (nb == 0)
					JOptionPane.showMessageDialog(null, "Ce tarif existe déjà !");
				else
				{
					JOptionPane.showMessageDialog(null, "Tarif ajouté.");
					this.lesTarifs.add(leTarif);
				}
			}
		}
	}
	
}
